import { queryCurrent } from '@/services/user';

const UserModel = {
  namespace: 'user',
  state: {
    currentUser: {},
  },
  effects: {
    *fetchCurrent(_, { call, put }) {
      const response = yield call(queryCurrent);

      if (response.code == 200) {
        const user = response.data.user;
        yield put({
          type: 'saveCurrentUser',
          payload: user,
        });
      }
    },
  },
  reducers: {
    saveCurrentUser(state, { payload }) {
      return { ...state, currentUser: payload };
    },
  },
};

export default UserModel;
